<?php
Class ModelCreditor
{
	public function getCreditorInfo($Creditorname, $password)
	{
		$sql = "SELECT admin_id, admin_firstname, admin_lastname, admin_email 
				FROM tbl_admin
				WHERE admin_Creditorname = '" . $Creditorname . "'
				AND admin_passwd = '" . md5($password) . "' 
				AND status = 1";
		$Creditor_arr = DB::fetchRow($sql);
		return $Creditor_arr;
		
	}
        public function getCreditorlist($limit)
        {
            $sql = "SELECT  * from tbl_user where usertype=2 and iscreditor=1 ";
            $sql .= $limit;
		$Creditor_arr = DB::fetchAll($sql);
		return $Creditor_arr;
        }

         public function gettotalrowcount()
    {


        $sql = "SELECT count(*) as count from tbl_user where usertype=2 and iscreditor=1 ";

        $count = DB::fetchRow($sql);

        return $count["count"];
    }

         public function saveCreditor($request)
        {
           if($this->isEmailexist($request["Creditor_email"])){
              exit( "<span class='error'>Creditor email already exist in the database</span>");
           }
           $sql = "insert into tbl_Creditor (Creditor_email,Creditor_passwd,Creditor_firstname,Creditor_lastname,Creditor_address,Creditor_city,Creditor_phone,Creditorfax,
                secret_question,secret_answer,status,Designation,companyname,companyaddress,typeofbusiness,businessregistration,
                industry,prefferedpayment,profession,nicno,Creditortype)
                values ('".$request["Creditor_email"] ."','".$request["Creditor_passwd"]."','".$request["Creditor_firstname"]."','".$request["Creditor_lastname"]."',
                '".$request["Creditor_address"]."','".$request["Creditor_city"]."','".$request["Creditor_phone"]."','".$request["Creditor_fax"]."','".$request["secret_question"]."'
                ,'".$request["secret_answer"]."','".$request["status"]."','".$request["Designation"]."',
                '".$request["companyname"]."','".$request["companyaddress"]."','".$request["typeofbusiness"]."','".$request["businessregistration"]."',
                '".$request["industry"]."','".$request["prefferedpayment"]."','".$request["profession"]."','".$request["nicno"]."','".$request["Creditor_type"]."') ";
         //  echo $sql;
		$insert = DB::exec($sql);
		if($insert)
		{
			echo "<span class='success'>Creditor details has been successfully updated</span>";
		}
        }

       

         function updateCreditor($request){
           // print_r($request);
           $sql="";
           if(strlen($request["Creditor_passwd"])>0){
               $sql= " update tbl_Creditor  set Creditor_passwd='".$request["Creditor_passwd"]."',Creditor_firstname='".$request["Creditor_firstname"]."',Creditor_lastname='".$request["Creditor_lastname"]."',Creditor_address='".$request["Creditor_address"]."',Creditor_city='".$request["Creditor_city"]."',Creditor_phone='".$request["Creditor_phone"]."',
                secret_question='".$request["secret_question"]."',secret_answer='".$request["secret_answer"]."',status=".$request["Status"].",Designation='".$request["Designation"]."',companyname='".$request["companyname"]."',companyaddress='".$request["companyaddress"]."',typeofbusiness='".$request["typeofbusiness"]."',businessregistration='".$request["businessregistration"]."',
               Creditorfax='".$request["Creditor_fax"]."', industry='".$request["industry"]."',Creditortype=".$request["Creditor_type"]." ,profession='".$request["profession"]."',Creditor_address='".$request["Creditor_address"]."' ,nicno='".$request["nicno"]."',prefferedpayment='".$request["prefferedpayment"]."' WHERE Creditor_id=".$request["Creditor_id"]."";
           }
           else{
               $sql= " update tbl_Creditor  set  Creditor_firstname='".$request["Creditor_firstname"]."',Creditor_lastname='".$request["Creditor_lastname"]."',Creditor_address='".$request["Creditor_address"]."',Creditor_city='".$request["Creditor_city"]."',Creditor_phone='".$request["Creditor_phone"]."',
                secret_question='".$request["secret_question"]."',secret_answer='".$request["secret_answer"]."',status=".$request["Status"].",Designation='".$request["Designation"]."',companyname='".$request["companyname"]."',companyaddress='".$request["companyaddress"]."',typeofbusiness='".$request["typeofbusiness"]."',businessregistration='".$request["businessregistration"]."',
               Creditorfax='".$request["Creditor_fax"]."', industry='".$request["industry"]."',Creditortype=".$request["Creditor_type"]." ,profession='".$request["profession"]."',Creditor_address='".$request["Creditor_address"]."' ,nicno='".$request["nicno"]."',prefferedpayment='".$request["prefferedpayment"]."' WHERE Creditor_id=".$request["Creditor_id"]."";
           }
           
    //echo $sql;
          $update = DB::exec($sql);
		if($update)
		{
			echo "<span class='success'>Creditor details has been successfully updated</span>";
		}
       }

        function getCreditorbyID($userid){

           //echo ($Creditorid);
            $sql=" select * from tbl_creditaccount where userid=".$userid." order by transdate asc";
            $Creditor_arr = DB::fetchAll($sql);
            return $Creditor_arr;
            
        }
        function isEmailexist($email){

           //echo ($Creditorid);
            $sql=" select * from tbl_Creditor where Creditor_email='".$email."'";
           // echo $sql;
            $Creditor_arr = DB::fetchAll($sql);
          //  print_r($Creditor_arr);
            return $Creditor_arr;

        }

       public function updatecredit($request){
             $type=$request["type"];
             $amount=$request["txtQty"];
             $userid=$request["userid"];

             if($this->creditbalance($userid)<$amount && $type==2 ){
                 $amount=$this->creditbalance($userid);
             }

             if($type==1){
                
                 $transtype=1;
             }
             else{
             
                 $transtype=2;
             }

             $sql=" insert into tbl_creditaccount (userid,transtype,transvalue,transdate) values  (".$userid.",".$type.",".$amount.",now()) ";

           //  echo $sql;
             $update = DB::exec($sql);
		if($update)
		{
			echo "<span class='success'>Credit value updated successfully !</span>";
		}
        }

        public function creditbalance($userid){
        $accountbalance=0;
        $sql = "SELECT  *  from  tbl_creditaccount  where userid =".$userid." ";
        $credit= DB::fetchAll($sql);
        foreach($credit as $arr){
         if(in_array($arr["transtype"], array(0,1,4,8))){
            $accountbalance+=(float)$arr["transvalue"];
         }
         else{
            $accountbalance-=(float)$arr["transvalue"];

         }
        }
        return $accountbalance;
    }
}
?>